package org.example.schoolbus.controller;
import org.example.schoolbus.entity.ResultBody;
import org.example.schoolbus.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/Register")
@CrossOrigin(origins ="*")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    @RequestMapping("/RegisterUser")
    public ResultBody RegisterUser(@RequestBody Map<String,Object> map) {
        System.out.println(map);
        if(map.get("userstyle")==null||map.get("username")==null||map.get("useremail")==null||map.get("userphone")==null||map.get("userpassword")==null)
        {
            return new ResultBody(404,"缺少信息",false);
        }
        String userstyle=map.get("userstyle").toString();//0为学生，1为管理员
        String username=map.get("username").toString();
        String useremail =map.get("useremail").toString();
        String userphone =map.get("userphone").toString();
        String userpassword =map.get("userpassword").toString();
        switch (userstyle)
        {
            case "0":
                if(registerService.RegisterUser("0",username,useremail,userphone,userpassword)>0) {
                return new ResultBody(200,"学生注册成功",true);
            }
            else {
                    return new ResultBody(400, "学生注册失败", false);
                }
            case "1":
                if(registerService.RegisterUser("1",username,useremail,userphone,userpassword)>0) {
                return new ResultBody(200,"管理员注册成功",true);
            }
            else {
                    return new ResultBody(400, "管理员注册失败", false);
                }
            default:
                return new ResultBody(400,"未知用户类型",false);
        }

    }


    }

